import { Injectable } from '@angular/core';
import { Subject, Observable } from 'rxjs';
@Injectable({
    providedIn: "root"
})
export class commService {
    constructor() { }
    //打开相机
    photo(width, height,flag=true): Observable<any> {
        window['androidObj'].photo(width, height);
        return new Observable(observer => {
            window['getPhotoImg'] = data => {
                if(flag){
                    return observer.next("data:image/png;base64," + data);
                }else{
                    return observer.next(data);
                }
               
            };
        })
    }

    //打开相册
    picture(width, height): Observable<any> {
        window['androidObj'].picture(width, height);
        return new Observable(observer => {
            window['getPhotoImg'] = data => {
                return observer.next("data:image/png;base64," + data);
            };
        })
    }

    //进入房间
    initRoom(roomId, peerId) {
        window['androidObj'].InitRoom(roomId, peerId);
    }

    //退出房间
    exitRoom() {
        console.log("退出房间")
        window['androidObj'].exitRoom();
    }

    //保存按钮
    downPic(url: String) {
        console.log(123456)
        window['androidObj'].downPic(url);
    }

    //打开相机
    QRCode(): Observable<any> {
        window['androidObj'].QRCode();
        return new Observable(observer => {
            window['getQRContent'] = content => {
                return observer.next(content)
            };
        })
    }

    //返回
    onAndroidBack(): Observable<any> {
        return new Observable(observer => {
            window['onAndroidBack'] = () => {
                return observer.next()
            };
        })
    }

    //退出
    closeActivity(): Observable<any> {
        return new Observable(observer => {
            window['androidObj'].closeActivity();
        })
    }

    loadMainUIOver(): Observable<any> {
        return new Observable(observer => {
            window['androidObj'].loadMainUIOver();
        })
    }

    aliPay(code): Observable<any> {
        window['androidObj'].aliPay(code);
        return new Observable(observer => {
            window['alipayResult'] = content => {
                return observer.next(content)
            };
        })
    }

    //获取网络
    initNewWork(): Observable<any> {
        window['androidObj'].initNewWork();
        return new Observable(observer => {
            return observer.next(window['androidObj'].getNetSpeed())
        })
    }


    /******开启人脸*********/
    startFaceCollect() {
        window['androidObj'].startFaceCollect();
        let flag=false;
        return new Observable(observer => {
            window['onAndroidCollectFaceImg'] = content => {
                if(flag){
                    return observer.next(content)
                }else{
                    setTimeout(()=>{
                        flag=true;
                    },1000)
                }
            };
        })
    }

    /******关闭人脸********/
    stopFaceCollect() {
        window['androidObj'].stopFaceCollect();
    }

        /**
         * @returns {Observable<any>} 
         * @memberOf 身份证正面
         */
     collectIDCardFront(): Observable<any> {
        window['androidObj'].collectIDCardFront();;
        return  new Observable(observer => {
            window['onAndroidcollectIDCardFront'] = content => {
                return observer.next("data:image/png;base64," +content)
            };
        }) 
   }

      /**
         * @returns {Observable<any>} 
         * @memberOf 身份证反面
         */
   collectIDCardBack(){
        window['androidObj'].collectIDCardBack();;
        return  new Observable(observer => {
            window['onAndroidcollectIDCardBack'] = content => {
                return observer.next("data:image/png;base64," +content)
            };
        }) 
   }

         /**
         * @returns {Observable<any>} 
         * @memberOf 版本信息
         */
        getVersionInfo(){
            return new Observable(observer => {
                return observer.next(window['androidObj'].getVersionInfo())
            })
       }

       /****
        * 
        * url
        *  window.androidObj.downAndInstallNewApk("https://sc.njguochu.com:46/package/app-debug.apk");
        *  */ 
       downFile(url){
             window['androidObj'].downAndInstallNewApk(url);
       }

   

}
